package com.qire.manhua.controller;

import android.view.MenuItem;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.support.base.EventController;
import com.qire.manhua.R;
import com.qire.manhua.BR;
import com.qire.manhua.activity.HomeActivity;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.HomeViewModel;

import androidx.annotation.NonNull;

/**
 * 首页控制器(Home)
 */
public class HomeController extends EventController<HomeActivity> {

    private HomeViewModel viewModel;

    public HomeController(HomeActivity activity) {
        super(activity);
    }

    @Override
    protected void bindView() {
        owner.viewDataBinder().setVariable(BR.homeController,this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(HomeViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**********************************************************************************************/

    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        if(ObjectUtil.nonNull(viewModel)) {
            switch (menuItem.getItemId()) {
                case R.id.app_home_menu_bookshelf:
                    viewModel.gotoTabPage(owner, PageTabIndex.TAB_INDEX_HOME_BOOKSHELF);
                    return true;
                case R.id.app_home_menu_home:
                    viewModel.gotoTabPage(owner, PageTabIndex.TAB_INDEX_HOME_RECOMMEND);
                    return true;
                case R.id.app_home_menu_classification:
                    viewModel.gotoTabPage(owner, PageTabIndex.TAB_INDEX_HOME_CLASSIFICATION);
                    return true;
                case R.id.app_home_menu_user:
                    viewModel.gotoTabPage(owner, PageTabIndex.TAB_INDEX_HOME_USER_CENTER);
                    return true;
            }
        }
        return false;
    }

    public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
        if(menuItem.getItemId() == R.id.app_home_menu_home && ObjectUtil.nonNull(viewModel)) {
            viewModel.gotoTabPage(owner, PageTabIndex.TAB_INDEX_HOME_RECOMMEND);
            return ;
        }
    }

}
